package tests;

import common.Utils;
import org.openqa.selenium.Alert;
import org.openqa.selenium.By;

import java.io.IOException;

/**
 * Created with IntelliJ IDEA.
 * Description:自动化测试博客系统登录页面
 * Author: WJW
 * Date: 2024-10-12
 * Time: 20:28
 */
public class LoginPage extends Utils {
    public static String url = "http://8.137.19.140:9090/blog_login.html";
    public LoginPage() {
        super(url);
    }

    public void loginPageRight(){
        //通过查看页面元素是否存在来检查页面加载成功与否
        driver.findElement(By.cssSelector("body > div.nav > a:nth-child(4)"));
        //登录输入框
        driver.findElement(By.cssSelector("body > div.container-login > div"));
    }

    //检查登录功能
    public void LoginSuc(){
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123456");
        driver.findElement(By.cssSelector("#submit")).click();
        //检查点击登录后登录是否成功
        //通过查看全文按钮
        driver.findElement(By.cssSelector("body > div.container > div.right > div:nth-child(1) > a"));
        //通过页面标题来检查是否登陆成功
        String except = driver.getTitle();
        assert except.equals("博客列表页");

        driver.navigate().back();
        driver.navigate().refresh();
    }

    //检查登录失败
    public void LoginFail() throws InterruptedException, IOException {
        driver.findElement(By.cssSelector("#username")).sendKeys("zhangsan");
        driver.findElement(By.cssSelector("#password")).sendKeys("123");
        driver.findElement(By.cssSelector("#submit")).click();


        //处理警告 弹窗
        //切换弹窗
        Thread.sleep(1000);
        Alert alert = driver.switchTo().alert();

        //点击确认
        alert.accept();
        //getScreenShot(getClass().getName());

       // driver.findElement(By.cssSelector("body > div.container-login > div > h3"));
    }
}
